//
//  YXDownloadCompletedTableViewCell.m
//  MockProject
//
//  Created by 周丽丽 on 16/8/24.
//  Copyright © 2016年 niuzhaowang. All rights reserved.
//

#import "YXDownloadCompletedTableViewCell.h"
#import "YXDownloader.h"
@interface YXDownloadCompletedTableViewCell ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *timeImageView;
@property (nonatomic, strong) UILabel *updateTimeLabel;
@property (nonatomic, strong) UILabel *totalSizeLabel;
@end
@implementation YXDownloadCompletedTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupSubviews];
    }
    return  self;
}
- (void)setupSubviews{
    UILabel *titleLbl = [[UILabel alloc]init];
    titleLbl.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview: titleLbl];
    self.titleLabel = titleLbl;
    
    UIImageView *timeImageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"时间icon"]];
    [self.contentView addSubview:timeImageV];
    self.timeImageView = timeImageV;
    
    UILabel *updateTimeLabl = [[UILabel alloc]init];
    updateTimeLabl.text = @"2016.8.23";
    updateTimeLabl.font = [UIFont systemFontOfSize:12];
    updateTimeLabl.textColor = [UIColor colorWithHexString:@"a3a1a0"];
    [self.contentView addSubview:updateTimeLabl];
    self.updateTimeLabel = updateTimeLabl;
    
    UILabel *totalSizeLbl = [[UILabel alloc]init];
    totalSizeLbl.font = [UIFont systemFontOfSize:12];
    totalSizeLbl.textColor = [UIColor colorWithHexString:@"a3a1a0"];
    [self.contentView addSubview:totalSizeLbl];
    self.totalSizeLabel = totalSizeLbl;

    UIButton *circleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    circleBtn.backgroundColor = [UIColor whiteColor];
    circleBtn.layer.cornerRadius = 12;
    circleBtn.layer.borderColor = [UIColor colorWithHexString:@"c1bab6"].CGColor;
    circleBtn.layer.borderWidth = 1;
    circleBtn.layer.masksToBounds = YES;
    [self.contentView addSubview:circleBtn];
    [circleBtn addTarget:self action:@selector(circleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.circleButton = circleBtn;
}
- (void)circleBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        sender.backgroundColor = [UIColor redColor];
        [sender setImage:[UIImage imageNamed:@"删除选择的对号图标"] forState:UIControlStateSelected];
    }else if (!sender.selected){
        sender.backgroundColor = [UIColor whiteColor];
        [sender setImage:[[UIImage alloc]init] forState:UIControlStateSelected];
    }
    if (self.circleButtonClickBlock) {
        self.circleButtonClickBlock(self);
    }
}
- (void)layoutSubviews{
    [super layoutSubviews];
    [self layoutFrame];
}
- (void)layoutFrame{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.circleButton).offset(-15);
        make.bottom.equalTo(self.contentView.mas_centerY);
    }];
    [self.circleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-15);
        make.centerY.equalTo(self.contentView);
        make.width.height.mas_equalTo(24);
    }];
    [self.timeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(13);
        make.left.equalTo(self.titleLabel);
    }];
    [self.updateTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.timeImageView);
        make.left.equalTo(self.timeImageView.mas_right).offset(5);
    }];
    [self.totalSizeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.updateTimeLabel);
        make.left.equalTo(self.updateTimeLabel.mas_right).offset(32);
    }];
}
#pragma mark - reload data
- (void)setDownloader:(YXDownloader *)downloader{
    _downloader = downloader;
    [self settingData];
}
- (void)settingData{
    self.titleLabel.text = self.downloader.title;
    self.totalSizeLabel.text = [NSString stringWithFormat:@"%.2fM",self.downloader.totalSize];
}
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextFillRect(context, rect);
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithHexString:@"e0dcd9"].CGColor);
    CGContextStrokeRect(context, CGRectMake(15, rect.size.height, rect.size.width - 10, 1));
}
@end
